package com.houtai.service.article.cate;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.RetInfo;
import com.houtai.bean.dto.article.comments.TsArticleCommentsListDto;
import com.houtai.bean.vo.article.comments.TsArticleCommentsList;
import com.houtai.bean.vo.article.comments.TsArticleCommentsReplyList;
import com.houtai.dao.TsArticleCommentsDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2017/4/22.
 */
@Service
public class ArticleCateService {
    private static Logger logger = Logger.getLogger(ArticleCateService.class);

    @Autowired
    private TsArticleCommentsDao tsArticleCommentsDao;

    @Autowired
    private RedisTemplate redisTemplate;




    /**
     * @Purpose:    文章评论列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ArticleListVo
     */
    public RetInfo commentsList(HttpServletRequest request, TsArticleCommentsListDto dto) {
        String logInfo = this.getClass().getName() + ":commentsList:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {

            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), "comments_id DESC");
            List<TsArticleCommentsList> lists = tsArticleCommentsDao.findTsArticleCommentsList(dto);
            PageInfo<TsArticleCommentsList> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            if (list.getTotal() > 0) {
                for (int i = 0; i < list.getList().size(); i ++){
                    List<TsArticleCommentsReplyList> reply = tsArticleCommentsDao.findTsArticleCommentsReplyList(list.getList().get(i).getComments_id());
                    list.getList().get(i).setReplys_size(reply.size());
                    list.getList().get(i).setReplys(reply);
                }



            }
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("article_id", dto.getArticle_id());
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
